package com.rules.admin.service.decision.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gillion.grule.client.utils.Streams;

import com.google.common.collect.Maps;
import com.google.common.base.Function;


import com.rules.admin.dao.DecisionTableDao;

import com.rules.admin.entity.DecisionCell;
import com.rules.admin.entity.DecisionColumnConfig;
import com.rules.admin.entity.DecisionRow;
import com.rules.admin.entity.DecisionTable;
import com.rules.admin.service.RulePackageService;
import com.rules.admin.service.RuleVariableService;
import com.rules.admin.service.decision.DecisionCellService;
import com.rules.admin.service.decision.DecisionColumnConfigService;
import com.rules.admin.service.decision.DecisionRowService;
import com.rules.admin.service.decision.DecisionTableService;
import com.rules.admin.vo.DecisionRowVO;
import com.rules.admin.vo.DecisionVO;
import com.rules.admin.vo.DecisionColConfig;
import com.rules.admin.vo.DecisionColVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class DecisionTableServiceImpl extends ServiceImpl<DecisionTableDao, DecisionTable> implements DecisionTableService {

   @Autowired
   private DecisionColumnConfigService decisionColumnConfigService;
   @Autowired
   private DecisionRowService decisionRowService;
   @Autowired
   private DecisionCellService decisionCellService;
   @Autowired
   private RuleVariableService ruleVariableService;
   @Autowired
   private RulePackageService rulePackageService;



   public List<Map<String, Object>> loadDecisionTable(Long rulePacageId) {
      List<DecisionTable> tables = this.getDecisionTablesByPackageId(rulePacageId);
      return Streams.of(tables).map(new Function<DecisionTable, Map<String, Object>>() {
         public Map<String, Object> apply(DecisionTable input) {
            Map<String, Object> table = Maps.newHashMap();
            table.put("id", input.getId() + "");
            table.put("name", input.getName());
            table.put("priority", input.getPriority());
            table.put("type", 1);
            return table;
         }
      }).collectToList();
   }

   public List<DecisionTable> getDecisionTablesByPackageId(long rulePacageId) {
//   DecisionTableExample example = new DecisionTableExample();
//   Criteria criteria = example.createCriteria();
//   criteria.andRulePackageIdEqualTo(rulePacageId);
//   example.setOrderByClause("priority desc, create_time asc");

      LambdaQueryWrapper<DecisionTable> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(DecisionTable::getRulePackageId, rulePacageId);
      queryWrapper.orderByDesc(DecisionTable::getPriority);
      queryWrapper.orderByAsc(DecisionTable::getCreateTime);
      return this.baseMapper.selectList(queryWrapper);
   }

   public List<DecisionTable> getDecisionTablesByPackageIdAndStatus(long rulePacageId) {
      LambdaQueryWrapper<DecisionTable> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(DecisionTable::getRulePackageId, rulePacageId);
      queryWrapper.eq(DecisionTable::getTableStatus,0);
      queryWrapper.orderByDesc(DecisionTable::getPriority);
      queryWrapper.orderByAsc(DecisionTable::getCreateTime);
      return this.baseMapper.selectList(queryWrapper);
   }





   public DecisionTable findByName(String name) {
      LambdaQueryWrapper<DecisionTable> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(DecisionTable::getName,name);
      return this.baseMapper.selectOne(queryWrapper);
   }

   public List<DecisionColVO> getCols(long tableId) {
      return this.baseMapper.getCols(tableId);
   }

   public void saveColConfig(long tableId, DecisionColConfig decisionColConfig) {
      String[] var4 = decisionColConfig.getAddColIds();
      int var5 = var4.length;

      int var6;
      String delColId;
      for(var6 = 0; var6 < var5; ++var6) {
         delColId = var4[var6];
         DecisionColumnConfig config = new DecisionColumnConfig();
         config.setTableId(tableId);
         config.setModelAttrId(Long.valueOf(delColId));
         this.decisionColumnConfigService.save(config);
      }

      var4 = decisionColConfig.getDelColIds();
      var5 = var4.length;

      for(var6 = 0; var6 < var5; ++var6) {
         delColId = var4[var6];
         this.decisionColumnConfigService.deleteByAttrId(Long.valueOf(delColId));
         this.decisionCellService.deleteByColId(Long.valueOf(delColId));
      }

   }

   public List<DecisionCell> getCells(long tableId) {
      return this.baseMapper.getCells(tableId);
   }

   public void saveDecision(DecisionVO decisionVO) {
      DecisionRowVO[] addRows = decisionVO.getAddRows();
      int var5;
      if (addRows != null && addRows.length > 0) {
         DecisionRowVO[] var3 = addRows;
         int var4 = addRows.length;

         for(var5 = 0; var5 < var4; ++var5) {
            DecisionRowVO addRow = var3[var5];
            addRow.setTableId(Long.valueOf(decisionVO.getTableId()));
            this.decisionRowService.addRow(addRow);
         }
      }

      DecisionRow[] updateRows = decisionVO.getUpdateRows();
      int var15;
      if (updateRows != null && updateRows.length > 0) {
         DecisionRow[] var12 = updateRows;
         var5 = updateRows.length;

         for(var15 = 0; var15 < var5; ++var15) {
            DecisionRow decisionRow = var12[var15];
            this.decisionRowService.saveOrUpdate(decisionRow);
         }
      }

      DecisionCell[] updateCells = decisionVO.getUpdateCells();
      DecisionCell[] existsRowAddCells;
      int var16;
      if (updateCells != null && updateCells.length > 0) {
         existsRowAddCells = updateCells;
         var15 = updateCells.length;

         for(var16 = 0; var16 < var15; ++var16) {
            DecisionCell updateCell = existsRowAddCells[var16];
            this.decisionCellService.saveOrUpdate(updateCell);
         }
      }

      existsRowAddCells = decisionVO.getExistsRowAddCells();
      int var19;
      if (existsRowAddCells != null && existsRowAddCells.length > 0) {
         DecisionCell[] var17 = existsRowAddCells;
         var16 = existsRowAddCells.length;

         for(var19 = 0; var19 < var16; ++var19) {
            DecisionCell existsRowAddCell = var17[var19];
            this.decisionCellService.save(existsRowAddCell);
         }
      }

      String[] delRowIds = decisionVO.getDelRowIds();
      if (delRowIds != null && delRowIds.length > 0) {
         String[] var20 = delRowIds;
         var19 = delRowIds.length;

         for(int var21 = 0; var21 < var19; ++var21) {
            String delRowId = var20[var21];
            this.decisionRowService.deleteRow(Long.parseLong(delRowId));
         }
      }

   }

   public void deleteDecision(long tableId) {
      this.decisionColumnConfigService.deleteByTableId(tableId);
      List<DecisionRow> rows = this.decisionRowService.getRowsByTableId(tableId);
      if (rows != null && !rows.isEmpty()) {
         Iterator var4 = rows.iterator();

         while(var4.hasNext()) {
            DecisionRow row = (DecisionRow)var4.next();
            this.decisionRowService.deleteRow(row.getId());
         }
      }

      this.ruleVariableService.deleteVariables(4, tableId);
      this.removeById(tableId);
   }

   public void create(DecisionTable decisionTable) {
      decisionTable.setPriority(this.rulePackageService.getPriority(decisionTable.getRulePackageId()));
      this.save(decisionTable);
   }
}
